﻿using Base.Api.Models;
using Base.Api.Service;
using Base.Data.Entity;
using Common.AspNetCore;
using Common.Infrastructure;
using Common.Infrastructure.Structs;
using Common.Models.Base;
using Common.RunModels.Base;
using Common.RunModels.FCP;
using Common.RunModels.OldModel;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Base.Api.Controllers
{
    [Route("api/Base")]
    public class BaseController : ApiController
    {
        private readonly BaseService baseService;
        private readonly BaseInitService initService;
        private readonly BaseLabelService baseLabelService;
        private readonly CheckWorkService checkWorkService;

        public BaseController(IServiceProvider provider, BaseService auth, BaseInitService init, BaseLabelService labelService, CheckWorkService check)
             : base(provider)
        {
            this.baseService = auth;
            this.initService = init;
            this.baseLabelService = labelService;
            this.checkWorkService = check;
        }

        [HttpPost, Route("FullUpdateMember")]
        public async Task<ApiActionResult<bool>> FullUpdateMember([FromBody] ModelSaveRequest request)
        {
            return await SafeExecuteAsync<bool>(async (param) =>
            {
                if (request.Details != null && request.Details.Any())
                {
                    var member = request.Details[0].ModelData.ToObjectFromJson<List<m_member>>();
                    if (member.Any())
                    {
                        return await this.baseService.FullUpdateMemberAsync(member.FirstOrDefault());
                    }
                }

                return true;
            });
        }

        [HttpPost, Route("FullUpdateRole")]
        public async Task<ApiActionResult<bool>> FullUpdateRole([FromBody] ModelSaveRequest request)
        {
            return await SafeExecuteAsync<bool>(async (param) =>
            {
                if (request.Details != null && request.Details.Any())
                {
                    var member = request.Details[0].ModelData.ToObjectFromJson<List<t_role>>();
                    if (member.Any())
                    {
                        await this.baseService.FullUpdateRoleAsync(member.FirstOrDefault());
                    }
                }

                return true;
            });
        }

        [HttpPost, Route("FullUpdateRoleGroup")]
        public async Task<ApiActionResult<bool>> FullUpdateRoleGroup([FromBody] ModelSaveRequest request)
        {
            return await SafeExecuteAsync<bool>(async (param) =>
            {
                if (request.Details != null && request.Details.Any())
                {
                    var member = request.Details[0].ModelData.ToObjectFromJson<List<t_role_group>>();
                    if (member.Any())
                    {
                        await this.baseService.FullUpdateRoleGroupAsync(member.FirstOrDefault());
                    }
                }

                return true;
            });
        }

        [HttpPost, Route("GetAllEnums")]
        public async Task<ApiActionResult<List<EnumInfo>>> GetAllEnums([FromBody] ApiParam apiParam)
        {
            return await SafeExecuteAsync<List<EnumInfo>>(async (param) =>
            {
                return await this.baseService.GetAllEnumsAsync(apiParam);
            });
        }

        [HttpPost, Route("GetEnumByStringInfosByEnumName")]
        public async Task<ApiActionResult<List<EnumByStringInfo>>> GetEnumByStringInfosByEnumName([FromBody] ApiParam apiParam)
        {
            return await SafeExecuteAsync<List<EnumByStringInfo>>(async (param) =>
            {
                return await this.baseService.GetEnumByStringInfosByEnumNameAsync(apiParam);
            });
        }

        [HttpPost, Route("InitOrg")]
        public async Task<ApiActionResult<bool>> InitOrg([FromBody] FCPInitParam initParam)
        {
            return await SafeExecuteAsync<bool>(async (param) =>
            {
                await this.initService.InitOrgAsync(initParam);
                return true;
            });
        }

        [HttpPost, Route("InitSys")]
        public async Task<ApiActionResult<bool>> InitSys([FromBody] FCPInitParam initParam)
        {
            return await SafeExecuteAsync<bool>(async (param) =>
            {
                await this.initService.InitSysAsync(initParam);
                return true;
            });
        }

        [HttpPost, Route("InitMemberPwd")]
        public async Task<ApiActionResult<bool>> InitMemberPwd([FromBody] ApiParam initParam)
        {
            return await SafeExecuteAsync<bool>(async (param) =>
            {
                await this.baseService.InitMemberPwdAsync(initParam);
                return true;
            });
        }

        [HttpPost, Route("ChangeMemberPwd")]
        public async Task<ApiActionResult<bool>> ChangeMemberPwd([FromBody] MemberPwdChangeParamModel initParam)
        {
            return await SafeExecuteAsync<bool>(async (param) =>
            {
                await this.baseService.ChangeMemberPwdAsync(initParam);
                return true;
            });
        }

        [HttpPost, Route("GetMemberById")]
        public ApiActionResult<object> GetMemberById([FromBody] ModelIdRequest modelRequest)
        {
            return SafeExecute<object>((param) =>
            {
                return this.baseService.GetMembersByIdsAsync(modelRequest).Result;
            });
        }

        [HttpPost, Route("SaveBaseLabelByMenu")]
        public async Task<ApiActionResult<bool>> SaveBaseLabelByMenu([FromBody] MenuLabelParamModel model)
        {
            return await SafeExecuteAsync<bool>(async (param) =>
            {
                return await this.baseService.SaveBaseLabelByMenuAsync(model);
            });
        }

        [HttpPost, Route("GetVueI18nMessage")]
        public async Task<ApiActionResult<VueI18nMessageResultModel>> GetVueI18nMessage([FromBody] VueI18nMessageParamModel model)
        {
            return await SafeExecuteAsync<VueI18nMessageResultModel>(async (param) =>
            {
                return await this.baseLabelService.GetVueI18nMessageAsync(model);
            });
        }

        [HttpPost, Route("GetLibMessageByApiStart")]
        public async Task<ApiActionResult<LibMessageApiResultModel>> GetLibMessageByApiStart([FromBody] LibMessageApiParamModel model)
        {
            return await SafeExecuteAsync<LibMessageApiResultModel>(async (param) =>
            {
                return await this.baseLabelService.GetLibMessageByApiStartAsync(model);
            });
        }

        [HttpPost, Route("GetLibMessageBySyncData")]
        public async Task<ApiActionResult<LibMessageApiResultModel>> GetLibMessageBySyncData([FromBody] LibMessageApiParamModel model)
        {
            return await SafeExecuteAsync<LibMessageApiResultModel>(async (param) =>
            {
                return await this.baseLabelService.GetLibMessageBySyncDataAsync(model);
            });
        }

        [HttpPost, Route("GetBaseLabelsByIds")]
        public async Task<ApiActionResult<List<m_base_label>>> GetBaseLabelsByIds([FromBody] ModelIdRequest model)
        {
            return await SafeExecuteAsync<List<m_base_label>>(async (param) =>
            {
                return await this.baseLabelService.GetBaseLabelsByIdsAsync(model);
            });
        }

        [HttpPost, Route("SaveBaseLabel")]
        public async Task<ApiActionResult<bool>> SaveBaseLabel([FromBody] ModelSaveRequest model)
        {
            return await SafeExecuteAsync<bool>(async (param) =>
            {
                return await this.baseLabelService.SaveBaseLabelAsync(model);
            });
        }

        [HttpPost, Route("GetBaseLabelOrderPage")]
        public async Task<ApiActionResult<ApiPagedResult<m_base_label>>> GetBaseLabelOrderPage([FromBody] ModelOrderPageRequest model)
        {
            return await SafeExecuteAsync<ApiPagedResult<m_base_label>>(async (param) =>
            {
                return await this.baseLabelService.GetBaseLabelOrderPageAsync(model);
            });
        }

        [HttpPost, Route("SaveVueI8Message")]
        public async Task<ApiActionResult<bool>> SaveVueI8Message([FromBody] SaveVueI18nMessageParamModel model)
        {
            return await SafeExecuteAsync<bool>(async (param) =>
            {
                return await this.baseLabelService.SaveVueI8MessageAsync(model);
            });
        }

        [HttpPost, Route("SystemTranslate")]
        public async Task<ApiActionResult<SystemTranslateResultModel>> SystemTranslate([FromBody] SystemTranslateParamModel model)
        {
            return await SafeExecuteAsync<SystemTranslateResultModel>(async (param) =>
            {
                return await this.baseLabelService.SystemTranslateAsync(model);
            });
        }

        /// <summary>
        /// 按工单确认对应的部署站点地址
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost, Route("GetUrlByCheckWork")]
        public async Task<JsonActionResult<UrlByCheckWorkResultModel>> GetUrlByCheckWork([FromBody] UrlByCheckWorkParamModel model)
        {
            JsonActionResult<UrlByCheckWorkResultModel> ret = new JsonActionResult<UrlByCheckWorkResultModel>();

            var r = await SafeExecuteAsync<UrlByCheckWorkResultModel>(async (param) =>
            {
                return await this.checkWorkService.GetUrlByCheckWorkAsync(model);
            });

            ret.IsSuccessful = r.IsSuccess;
            ret.Data = r.Data;
            if (r.Messages.Any(t => t.MessageType == Common.Infrastructure.EFMessageType.Error))
            {
                ret.ErrorMessage = "<MES>" + string.Join(",", r.Messages.Select(t => t.Message));
                ret.ErrorTrack = r.Messages.FirstOrDefault(t => t.MessageType == EFMessageType.Error)?.StackTrace;
            }

            return ret;
        }
    }
}
